<?php

/**
 * ElectricityMsgtpl寝室电费充值消息模板
 *
 * @author Administrator
 */
class ElectricityMsgtpl extends BaseMsgtpl
{
	/**
	 * 电费预警发消息给学生
	 * @param int $info  信息
	 * @param int $ele  剩余电量
	 * @param int $maxEle  预警电量
	 * @return bool
	 */
	public function warningSendMessage($info,$ele,$maxEle)
	{
//		$wechatUserInfo = D('Wechat/WechatUser')->getInfo(array('user_id' => $info['user_id']),'openid,subscribe');
		$url = C('site_url').'/base/electricity/index/ele_id/1';
		//查询用户信息
//		$userInfo = D('User')->getByPk($info['user_id']);
//		$roomInfo = D('RoomView')->getInfo(array('room_id' => $info['room_id']));
		//修改为查询关注寝室的用户
    	$roomInfo = D('FloowUserView')->getList(array('room_id'=>$info['room_id']));

			foreach($roomInfo as $ri =>$i){
			    if($i['status']==0 && $i['cut_off']==0){
                    $wechatUserInfo = D('Wechat/WechatUser')->getInfo(array('user_id' => $i['user_id']),'openid,subscribe');
                    if ($wechatUserInfo['subscribe']) {
                        $this->wxTplSign = 'YMsmjeuenEBhlDJ3sXpDuklE-mB3CB3P1B6HwnKwF18';
                        $info = array(
                            'first' => array("value" => '同学，你的寝室电量剩余不足'.$maxEle.'度,请及时充值'),
                            'keynote1' => array("value" => $i['real_name']),
                            'keynote2' => array("value" => $i['community_name'].$i['building_name'].$i['room_name'].'室'),
                            'keynote3' => array("value" => $ele),
                            'remark' => array("value" => '请点此登录电费充值模块进行充值,若不想接收此消息，可进提醒设置关闭。'),
                        );
                        $this->wxObj->messageTpl($this->wechatInfo['appid'], $this->wechatInfo['appsecret'], $wechatUserInfo['openid'], $this->wxTplSign, $info, $url);
                    }else{
                        continue;
                    }
                }else{
			        continue;
                }

		}
	}

	/**
	 * 给用户发消息
	 */
	public function sendMessageToUser($orderInfo,$accountId)
	{
		$wechatUser = D('Wechat/WechatUser')->getInfo(array('user_id' => $orderInfo['user_id']), 'openid,subscribe');
		//查询学生信息
		$user = D('User')->getByPk($orderInfo['user_id']);

		if($wechatUser['subscribe']) {
			if($accountId == 2) { //滨江后勤
                $url = C('site_url').'/base/electricity/chargeLog/ele_id/'.$orderInfo['ele_id'];
				$this->wxTplSign = 'get7zdnd9oHLwXcZzUGKnGuP54vT386IVneVSVDkTnQ';
			}elseif($accountId == 25){ //杭州电子科技大学
                $this->wxTplSign = 'pA6DWwwJWGYNWWSYCHUJh97ZBvqfEYdmjoV_FFe67fs';
                $url = C('site_url').'/base/electricityHd/chargeLog/ele_id/'.$orderInfo['ele_id'];
            }

			$info = array(
				'first' => array("value" => '你已成功缴纳电费！！'),
				'keyword1' => array("value" => $user['real_name']),
				'keyword2' => array("value" => $orderInfo['order_amount'])." 元",
				'keyword3' => array("value" => date('Y-m-d H:i')),
				'remark' => array("value" => '详情请登录系统查看'),
			);

			$this->wxObj->messageTpl($this->wechatInfo['appid'], $this->wechatInfo['appsecret'], $wechatUser['openid'], $this->wxTplSign, $info, $url);
		}
	}

	/**
	 * 订单异常给管理员发消息
	 */
	public function sendMessageToManage($orderInfo,$accountId)
	{
		$wechat = D('Wechat')->get($orderInfo['wechat_id']);
		session('account_id', $wechat['account_id']);
		$fans = D('Fans')->get(345, 'openid,subscribe,wechat_id');
		if($fans['subscribe']) {
			
			if($wechat['wechat_id'] == 14) {
				$this->wxTplSign = 'get7zdnd9oHLwXcZzUGKnGuP54vT386IVneVSVDkTnQ';
			} elseif($wechat['wechat_id'] == 17) {
				$this->wxTplSign = 'devKooljmH_iKIuGKZe2ulByeiiOJVzvcJUvkG5UTDs';
			}

			$str1 = $orderInfo['community_name'] . $orderInfo['building_name'] . $orderInfo['room'] . ' ' . $orderInfo['username'] . '充值' . $orderInfo['order_price'] . '元电费，由于网络原因，该订单已付款但未加电成功，请前往后台进行核实处理！';
			$info = array(
				'first' => array("value" => $str1, 'color' => '#173177'),
				'keyword1' => array("value" => $orderInfo['username']),
				'keyword2' => array("value" => $orderInfo['order_price'], "color" => "#173177"),
				'keyword3' => array("value" => date('Y-m-d H:i'), "color" => "#173177"),
				'remark' => array("value" => '详情请登录系统查看'),
			);
			$this->wxObj->messageTpl($wechat['appid'], $wechat['appsecret'], $fans['openid'], $this->wxTplSign, $info);
		}
	}
}
